package com.my.mynote;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.my.bean.Note;
import com.my.helper.NoteDbOpenHelper;
import com.my.util.ToastUtil;

import java.text.SimpleDateFormat;
import java.util.Date;

public class EditActivity extends AppCompatActivity {
    private Note mNote;
    private EditText etTitle,etContent;
    private NoteDbOpenHelper mNoteDbOpenHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit);
        etTitle = findViewById(R.id.et_titles);
        etContent = findViewById(R.id.et_contents);

        initData();
    }

    private void initData() {
        //参数的传递
        Intent intent = getIntent();
        mNote = (Note) intent.getSerializableExtra("note");
        if (mNote!=null){
            etTitle.setText(mNote.getTitle());
            etContent.setText(mNote.getContent());
        }
        mNoteDbOpenHelper = new NoteDbOpenHelper(this);
    }

    public String getCurrentTimeFormat(){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd HH:mm:ss");
        Date date = new Date();
        return simpleDateFormat.format(date);
    }

    public void update(View view) {
        String title = etTitle.getText().toString().trim();
        String content = etContent.getText().toString().trim();
        if (TextUtils.isEmpty(title)){
            ToastUtil.showShortToast(this,"标题不能为空");
            return;
        }

        mNote.setTitle(title);
        mNote.setContent(content);
        mNote.setCreate(getCurrentTimeFormat());
        int row = mNoteDbOpenHelper.updateData(mNote);
        if (row>0){
            ToastUtil.showLongToast(this,"修改成功！");
            //添加成功之后要自动返回到列表界面
            this.finish();
        }else {
            ToastUtil.showLongToast(this,"修改失败！");
        }
    }
}